import time
from datetime import datetime

import pandas
import pandas as pd
import geopandas as geo
import matplotlib.pyplot as plt
from math import cos, sin, atan2, sqrt, pi, radians, degrees

# pandas show all rows
pd.set_option('display.max_rows', None)
# pandas show all col
pd.set_option('display.max_columns', 20)
# dataframe打印不换行
pd.set_option('display.width', 5000)

#####################################################
# clean data Second     订单清洗第二步
# cleans orders where the origin and destination not in the grid        清除起点和终点不在站点网格的订单
# 1清除od不在网格内的订单
# 2给订单增加两列，分别是上车网格id，下车网格id
# 3清除上车和下车在同一个网格的订单
#####################################################

def ptype(file):
    print(type(file))

def pd_show(file):
    print(file.shape)
    print(file.head(3))

# draw board
fig = plt.figure(1, (16, 9), dpi=120)
ax = plt.subplot(111)
plt.sca(ax)



start = time.time()

# read grid geo
grid = geo.read_file('../data/myz/grid_merge.shp')
grid.plot(ax=ax, edgecolor=(0, 0, 0, 1), facecolor=(0, 0, 0, 0), linewidths=0.5)
pd_show(grid)

# read taxi order data
order = pd.read_csv('E:/DataSet/taxi_data.csv')
pd_show(order)

# clean orders where origin not in the grid
# build GeoDataframe
order = geo.GeoDataFrame(order, geometry=geo.points_from_xy(order.up_lon, order.up_lat))
# print(type(order))
# print(order.head())

# get grid border
border = grid.loc[0, 'geometry']
print('清除o不在网格的订单')
order = order[order.geometry.within(border)]
pd_show(order)

# delete the geometry of the order
order.drop(['geometry'], axis=1, inplace=True)
order = geo.GeoDataFrame(order, geometry=geo.points_from_xy(order.off_lon, order.off_lat))

# clean orders where destination not in grid
print('清除d不在网格的订单')
order = order[order.geometry.within(border)]
order.drop(['geometry'], axis=1, inplace=True)
pd_show(order)

# draw order origin on ax
# plt.scatter(order['up_lon'], order['up_lat'], marker='o', alpha=0.5, s=2, c='green')
# plt.scatter(order['off_lon'], order['off_lat'], marker='o', alpha=0.5, s=2, c='red')
# plt.plot(order['up_lon'], order['up_lat'],order['off_lon'], order['off_lat'],'red')
# plt.scatter(x=taxi['pickup_longitude'],y=taxi['pickup_latitude'],marker='o',s=5, c='green')
# order.plot(ax=ax, edgecolor=(0, 0, 0, 1), facecolor=(1, 0, 0, 1), linewidths=0, markersize=20)
# order.to_csv('E:\DataSet\zipcar_now\order.csv', index=False)
# count program runtime

# 1.read order file
print('为od都在网格的订单划分od对应的网格id')
# 用户上车网格id
order['up_id'] = -1
# 用户下车网格id
order['off_id'] = -1
pd_show(order)

# read grid geodataframe
print('读取网格站点文件')
grid = geo.read_file('../data/myz/res_station.shp')

order_o = geo.GeoDataFrame(order, geometry=geo.points_from_xy(order.up_lon, order.up_lat))
# print(order_o.head())

print('为订单出发地划分网格')
for index, row in grid.iterrows():
    bord = row['geometry']
    order_o.loc[order_o.geometry.within(bord),'up_id'] = row['id']

order_d = geo.GeoDataFrame(order_o, geometry=geo.points_from_xy(order_o.off_lon, order_o.off_lat))
# print(order_d.head())
print('为订单到达地划分网格')
for index, row in grid.iterrows():
    bord = row['geometry']
    order_d.loc[order_d.geometry.within(bord),'off_id'] = row['id']

order_d.drop(['geometry'], axis=1, inplace=True)


# delete rows of up_id equeals off_id   删除出发和到达是同一个网格的订单
print('删除od在同一个网格的订单')
order_d.drop(order_d[order_d.up_id == order_d.off_id].index, inplace=True)
pd_show(order_d)

# print('绘制订单od图')
# mp = geo.read_file('./map/NewYork.shp')
# mp.plot(ax=ax, edgecolor=(0, 0, 0, 1), facecolor=(1, 0, 0, 0), linewidths=0.5)
# temp = geo.read_file('./myz/res_station.shp')
# temp.plot(ax=ax, edgecolor=(0, 0, 0, 1), facecolor=(1, 0, 0, 0), linewidths=0.5)
#
# plt.scatter(order['up_lon'], order['up_lat'], marker='o', alpha=0.5, s=2, c='green')
# plt.scatter(order['off_lon'], order['off_lat'], marker='o', alpha=0.5, s=2, c='red')

order_d.to_csv('./myz/order.csv', index=False)

# count program runtime
print('run time:', time.time() - start, 's')



# show draw board
plt.show()
